package com.ljh.controller;


import com.ljh.common.JsonData;
import com.ljh.pojo.Billdefail;
import com.ljh.pojo.Billtable;
import com.ljh.pojo.Footthebill;
import com.ljh.pojo.Member;
import com.ljh.service.BILLTABLEervice;
import com.ljh.service.MEMBERervice;
import com.ljh.utils.Integrating;
import com.ljh.vo.BillVo;
import com.ljh.vo.BilltableVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘剑鸿
 * @since 2021-11-09
 */
@Controller
@RequestMapping("/billtable")
public class BilltableController {
    @Autowired
    private BILLTABLEervice billtablEervice;
    @Autowired
    private MEMBERervice membeRervice;

    @RequestMapping("ToBill")
    public String ToBill(){
        return "Bill/bill";
    }
    @RequestMapping("SearchBill")
    @ResponseBody
    public JsonData loadBill(BilltableVo billtableVo){

        return billtablEervice.SearchBill(billtableVo);
    }

    @RequestMapping("OutBill")
    public String OutBill(Billtable billtable, Model model){
        Integer BillId = billtable.getBillid();
         BillVo<Billtable> bill = billtablEervice.getBill(BillId);
        //

        model.addAttribute("bill",bill);
        return "Bill/outBill";
    }
    @RequestMapping("abbfootbill")
    @ResponseBody
    public JsonData abbfootbill(Footthebill footthebill,Member member){

        BigDecimal zong = new BigDecimal(0);
        BigDecimal zhao = new BigDecimal(0);
        zong = footthebill.getZonge().multiply(footthebill.getRatio());
        BigDecimal bigDecimal = footthebill.getCollect();
        zhao = bigDecimal.subtract(zong);
        System.out.println("总额"+zong);
        System.out.println("收取"+bigDecimal);
        System.out.println("找零"+zhao);
        String BillNum = footthebill.getBillnumber();
        Billtable billtable = billtablEervice.TogetBill(BillNum);
        footthebill.setBillid(billtable.getBillid());
        footthebill.setClientid(billtable.getClientid());
        footthebill.setId(billtable.getId());

        System.out.println(footthebill);
        try{
            if (footthebill.getMemberid() > 0){

                Integer jifen = Integrating.toInt(footthebill.getZonge());
                Member integral = membeRervice.getmemberId(footthebill.getMemberid());
                Integer foint =  integral.getIntegral();//获取j积分
                BigDecimal Balan = integral.getBalance();//获取已余额
                Integer allint = jifen + foint;
                System.out.println("总积分"+allint);
                member.setIntegral(allint);
                Integer gradeId = changamember(allint);//会员升级
                member.setMemberGradeid(gradeId);
                if (footthebill.getSwitchv() > 0){//将找零加入余额
                    member.setBalance(Balan.add(zhao));
                }
                /*if(footthebill.getSwitchb() == 1){ ////消费大于余额 ----->余额修改为0
                    BigDecimal bigDec = new BigDecimal(0);
                    member.setBalance(bigDec);
                }
                if(footthebill.getSwitchb() == 2){ //余额大于消费  ------>  修改余额
                   BigDecimal Balans = Balan.subtract(footthebill.getActual());
                   member.setBalance(Balans);
                }*/
                membeRervice.updataIntSave(member);

            }
            billtablEervice.AddFoot(footthebill);
            return JsonData.success("结账完成");
        }
            catch (Exception e){
                return JsonData.fail(e.getMessage());
            }
    }
    public Integer changamember(Integer allint){
        Integer innt;
        if (allint == 0){
            innt = 0;
        }
        else if(allint >= 200 && allint < 500){
            /*修改会员*/
            innt = 1;
        }
        else if(allint >= 500 && allint < 1000){
            /*修改会员*/
            innt = 2;
        }
        else if(allint >= 1000 && allint < 2500){
            /*修改会员*/
            innt = 3;
        }
        else if(allint >= 2500 && allint < 5000){
            /*修改会员*/
            innt = 4;
        }
        else if(allint >= 5000 && allint < 1000){
            /*修改会员*/
            innt = 5;
        }
        else if(allint >= 10000 ){
            /*修改会员*/
            innt = 6;
        }
        else{
            innt = 7;
        }
        return innt;
    }
}